import CommonAPI from "@/api/system/common";

// 查询参数
export interface queryParams {
    page: number;
    limit: number;
    total: number;
}

// 查询参数
export interface btnObj {
    name: string;
    iconName: string;
    perms: string;
    isPlain: boolean;
}

// 查询参数
export interface reset {
    reset: boolean;
}

// 工具列表
export const btnList: Array<btnObj> = [
    {
        name: "添加用户",
        iconName: "user",
        perms: "sys:user:addUser",
        isPlain: false,
    },
    {
        name: "批量导入",
        iconName: "Fold",
        perms: "sys:user:batchImport",
        isPlain: true,
    },
    {
        name: "导出",
        iconName: "Expand",
        perms: "sys:user:export",
        isPlain: true,
    },
];

export const tableHead = reactive([
    {
        prop: "id",
        label: "ID",
    },

    {
        prop: "username",
        label: "用户名",
        width: "250",
    },
    {
        prop: "realname",
        label: "真实姓名",
    },
    {
        prop: "phone",
        label: "手机号",
    },
    {
        prop: "type_name",
        label: "用户类型",
    },
    {
        prop: "company_name",
        label: "所属单位",
    },

    {
        prop: "status",
        label: "状态",
        width: "120",
    },

    {
        prop: "create_time",
        label: "创建时间",
        width: "250",
    },
    {
        prop: "",
        label: "操作",
        width: "300",
        align: "center",
    },
]);
export const toDoRow = reactive([
    {
        name: "查看",
        icon: "View",
    },
    {
        name: "编辑",
        icon: "edit",
    },
    {
        name: "重置密码",
        icon: "Refresh",
    },
]);

// 用户查询表单验证规则
export const userFromRules = reactive({
    user_name: [{ required: true, message: "请输入用户信息", trigger: "blur" }],
    user_type_id: [
        { required: true, message: "请选择用户类型", trigger: "blur" },
    ],
    compony_id: [
        { required: true, message: "请选择地方区域", trigger: "blur" },
    ],
    status: [{ required: true, message: "选择用户状态", trigger: "blur" }],
    address: [{ required: true, message: "请输入详细地址", trigger: "blur" }],
});
// 添加用户表单验证规则
export const addUserFromRules = reactive({
    realname: [{ required: true, message: "请输入真实姓名", trigger: "blur" }],
    phone: [{ required: true, message: "请输入手机号码", trigger: "blur" }],
    part: [{ required: true, message: "请选择所属地域", trigger: "blur" }],
    compony_id: [
        { required: true, message: "请选择所属单位", trigger: "blur" },
    ],
});

// 用户状态
export const userStatusOption = ref([
    {
        label: "禁用",
        value: 0,
    },
    {
        label: "正常",
        value: 1,
    },
]);

export const baseInfoMap = [
    {
        label: "真实姓名",
        key: "realname",
    },
    {
        label: "手机号",
        key: "phone",
    },
    {
        label: "所属单位",
        key: "compony_name",
    },
    {
        label: "所属区域",
        key: "region",
    },
    {
        label: "详细地址",

        value: "data.address",
    },
];

export const leavelInforMap = [
    {
        label: "上级人员",
        key: "parent",
    },
    {
        label: "下级人员",
        key: "children",
    },
];

const permissionInMap = [
    {
        label: "用户角色",
        key: "user_type_name",
    },
    {
        label: "当前",
        key: "count",
    },
];
const createdInMap = [
    {
        label: "创建时间",
        key: "create_time",
    },
    {
        label: "创建人",
        key: "create_username",
    },
    {
        label: "最近修改时间",
        key: "update_time",
    },
];

const infoMap = new Map([
    ["基本信息", baseInfoMap],
    ["上下级信息", leavelInforMap],
    ["权限信息", permissionInMap],
    ["创建信息", createdInMap],
]);

export const formTitleList = Array.from(infoMap.keys());

export const getInfoList = (
    data: any,
    nameSting: "基本信息" | "上下级信息" | "权限信息" | "创建信息"
) => {
    let keyList = infoMap.get(nameSting);
    return keyList!.map((item: any) => {
        return {
            label: item.label,
            value: data[item.key],
        };
    });
};

// 获取省市区地市县列表  镇 村
export async function getLeaveOneToThreeOption(node: any) {
    let api = CommonAPI.getDistrictList;
    let params: any = {
        region_code: node.value,
    };
    if (node.level <= 2) {
        params.level = node.level + 1;
    }
    if (node.level === 3) {
        api = CommonAPI.getTownList;
    }
    if (node.level === 4) {
        api = CommonAPI.getVillageList;
    }
    return new Promise(async (resolve, reject) => {
        await api(params)
            .then((data: any) => {
                let option = data.map((item: any) => {
                    return {
                        label: item.region_name,
                        value: item.region_code,
                        leaf: node.level >= 4,
                    };
                });
                resolve(option);
            })
            .catch((err: any) => {
                console.log(err);
            });
    });
}
